//
//  BubbleMenuView.swift
//  TimMusic
//
//  Created by nkl on 2024/10/14.
//

import Foundation
import UIKit
import KLExtension

enum ImportSource {
    case file // 文件
    case library // 相册
}

class BubbleMenuView: UIView {
    enum Position {
        case topLeft
        case topRight
    }

    lazy var stackView: UIStackView = {
        let v = UIStackView()
        v.axis = .vertical
        v.alignment = .fill
        v.distribution = .fill
        return v
    }()

    lazy var videoButton = UIButton()
    lazy var fileButton = UIButton()

    var actionHanlder: ((ImportSource) -> Void)?
    var position: Position = .topLeft

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear

        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.leading.equalTo(12)
            make.trailing.equalTo(-12)
            make.bottom.equalToSuperview()
            make.top.equalTo(8)
            make.width.equalTo(164)
        }

        stackView.addArrangedSubview(videoButton)
        videoButton.snp.makeConstraints { make in
            make.height.equalTo(44)
        }

        let line = UIView()
        line.backgroundColor = .white.withAlphaComponent(0.2)
        stackView.addArrangedSubview(line)
        line.snp.makeConstraints { make in
            make.height.equalTo(1)
            make.width.equalTo(164)
        }

        stackView.addArrangedSubview(fileButton)
        fileButton.snp.makeConstraints { make in
            make.height.equalTo(44)
        }

        videoButton.setLocalizedImageAndTitle(image: UIImage(named: "ic-ring-import-video"), title: "From Video Library".localized(), for: .normal)
        videoButton.setTitleColor(.white, for: .normal)
        videoButton.titleLabel?.font = .systemFont(ofSize: 14)

        fileButton.setLocalizedImageAndTitle(image: UIImage(named: "ic-ring-import-file"), title: "From Files".localized(), for: .normal)
        fileButton.setTitleColor(.white, for: .normal)
        fileButton.titleLabel?.font = .systemFont(ofSize: 14)

        videoButton.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
        fileButton.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        drawPath()
    }

    func drawPath() {
        guard frame.size != .zero else {
            return
        }

        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: stackView.y, width: width, height: stackView.frame.height), cornerRadius: 8)

        switch position {
        case .topLeft:
            path.move(to: CGPoint(x: 24, y: stackView.y))
            path.addLine(to: CGPoint(x: 24 + 8, y: 0))
            path.addLine(to: CGPoint(x: 24 + 16, y: stackView.y))
        case .topRight:
            path.move(to: CGPoint(x: width - 10, y: stackView.y))
            path.addLine(to: CGPoint(x: width - 10 - 8, y: 0))
            path.addLine(to: CGPoint(x: width - 10 - 16, y: stackView.y))
        }
        path.close()

        let shapeLayer = CAShapeLayer()
        shapeLayer.fillColor = UIColor.black.cgColor
        shapeLayer.path = path.cgPath
        layer.insertSublayer(shapeLayer, at: 0)
    }

    @objc func buttonClick(_ sender: UIButton) {
        switch sender {
        case videoButton:
            actionHanlder?(.library)
        case fileButton:
            actionHanlder?(.file)
        default:
            break
        }
    }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = super.hitTest(point, with: event)
        if !bounds.contains(point) {
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                self.removeFromSuperview()
            }
        }
        return view
    }
}

